package com.dgut.legalservice.controller;

import com.dgut.legalservice.domain.User;
import com.dgut.legalservice.service.UserService;
import com.dgut.legalservice.vCode.FileUpLoad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

@Controller
@RequestMapping("/platForm")
public class UserController {
    @Autowired
    private UserService userServiceImpl;

    @RequestMapping(value = "/updateSuper", method = RequestMethod.POST) //修改超级管理员基本信息
//    public String updateBase(User user, HttpSession session, MultipartFile photoAtt, HttpServletRequest request) throws IOException {
    public String updateBase(User user, HttpSession session, HttpServletRequest request) throws IOException {
        User user1 = (User) session.getAttribute("user");
        FileUpLoad file = new FileUpLoad();
//        String name = photoAtt.getOriginalFilename();
//        if(!name.equals("")){   //不传头像的情况
//            user1.setPhotoAttId(file.fileUpload(photoAtt, request,session));  //设置头像url
//        }
        user1.setRealName(user.getRealName());
        user1.setSex(user.getSex());
        user1.setEmail(user.getEmail());
        user1.setType(user.getType());
        user1.setPhoneNum(user.getPhoneNum());
        userServiceImpl.updateSuper(user1);//更新t_sys_user表
        session.setAttribute("user",user1);  //更新session
        return "/platform/user-info";
    }

    @RequestMapping("/updatePassword")  //修改超级管理员密码
    public String updatePassword(String newPassword,HttpSession session){
        System.out.println(newPassword);
        User user = (User) session.getAttribute("user");
        user.setPassword(newPassword);
        userServiceImpl.updatePassword(user); //更新数据库的password
        session.setAttribute("user",user);//更新session
        return "/platform/password-update";
    }


    @RequestMapping("/listUser")
    public String listUser(){
        return "/platform/user-list";
    }

    @RequestMapping("/findNotSuper")
    public ModelAndView findNotSuper(){  //查询非超级管理员的所有用户
        ModelAndView mv = new ModelAndView();
        List<User> lists= userServiceImpl.findNotSuper();
        mv.setViewName("/platform/user-list");
        mv.addObject("lists",lists);
        return mv;
    }
    @RequestMapping("/findNormal")
    public ModelAndView findNormal(){  //查询非超级管理员的所有用户
        ModelAndView mv = new ModelAndView();
        List<User> lists= userServiceImpl.findNormal();
        mv.setViewName("/platform/user-list");
        mv.addObject("lists",lists);
        return mv;
    }


    @RequestMapping("/findByNameType")
    public ModelAndView findByNameType(HttpSession session,String realName,String option){
        ModelAndView mv = new ModelAndView();
        User user = (User) session.getAttribute("user");
        String type ;  //查询的类型
        List<User>lists;  //user
        if (option.equals("#")){
            if (realName.equals("")){   //用户名和选项均为空
                if (user.getType().equals("管理员"))
                    lists = userServiceImpl.findNormal();
                else
                    lists = userServiceImpl.findNotSuper();
            }else{  //用户名不为空
                lists = userServiceImpl.findByRealName(realName);
            }
        }else{
            if (option.equals("2")){
                type="普通用户";
            }else if (option.equals("3")){
                type="服务商";
            }else {
                type="管理员";
            }
            if (realName.equals("")){  //用户名为空，类型不为空
                lists = userServiceImpl.findByType(type);
            }else{
                lists = userServiceImpl.findByNameAndType(realName,type);
            }
        }
        mv.setViewName("/platform/user-list");
        mv.addObject("lists",lists);
        return mv;
    }

    @RequestMapping("/addUser")  //添加新账号
    public ModelAndView addUser(User user){
        ModelAndView mv = new ModelAndView();
        user.setPhotoAttId("../img/头像.png");
        System.out.println("user"+user);
        userServiceImpl.addUser(user);   //添加
        User user1= userServiceImpl.findByLoginName(user.getLoginName());  //显示添加的用户
        System.out.println("user1"+user1);
        mv.setViewName("/platform/user-list");
        mv.addObject("lists",user1);
        return mv;
    }
    @RequestMapping("/deleteUser")  //删除账号
    public ModelAndView deleteUser(Integer id){
        ModelAndView mv = new ModelAndView();
        userServiceImpl.deleteUser(id);   //删除
        List<User> lists= userServiceImpl.findNotSuper(); //删除用户
        mv.setViewName("/platform/user-list");
        mv.addObject("lists",lists);
        return mv;
    }
    @RequestMapping("/edit")
    public ModelAndView editUser(User user){
        ModelAndView mv = new ModelAndView();
        userServiceImpl.editUser(user); //编辑用户
        User user1 = userServiceImpl.findById(user.getId());
        mv.addObject("lists",user1);
        mv.setViewName("/platform/user-list");
        return mv;
    }
    @RequestMapping("/ice")
    public ModelAndView ice(Integer id){
        ModelAndView mv = new ModelAndView();
        userServiceImpl.ice(id);  //冻结账号
        User user1 = userServiceImpl.findById(id);
        mv.addObject("lists",user1);
        mv.setViewName("/platform/user-list");
        return mv;
    }
    @RequestMapping("/unIce")
    public ModelAndView unIce(Integer id){
        ModelAndView mv = new ModelAndView();
        userServiceImpl.unIce(id);   //解冻账号
        User user1 = userServiceImpl.findById(id);
        mv.addObject("lists",user1);
        mv.setViewName("/platform/user-list");
        return mv;
    }
    @RequestMapping("/resetPwdById")
    public ModelAndView resetPwdById(Integer id1,String newPassword){
        ModelAndView mv = new ModelAndView();
        userServiceImpl.resetPwdById(id1,newPassword);  //重置密码
        User user1 = userServiceImpl.findById(id1);
        mv.addObject("lists",user1);
        mv.setViewName("/platform/user-list");
        return mv;
    }

}
